﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BlawkayEngine;

namespace BlawkayHockey
{
    public class Grid
    {

        #region Constructors

        public Grid(Game oGame)
        {
            Reset(oGame);
        }

        public void Reset(Game oGame)
        {
            m_oEffect = new BasicEffect(oGame.GraphicsDevice);
            m_oEffect.World = Matrix.Identity;
            m_oEffect.VertexColorEnabled = true;

            CreateLines();
        }

        #endregion

        #region Methods

        public void Draw(Game oGame, Camera oCamera)
        {
            GraphicsDevice oDevice = oGame.GraphicsDevice;
            m_oEffect.View = oCamera.View;
            m_oEffect.Projection = oCamera.Projection;

            VertexBuffer vb = new VertexBuffer(oDevice, VertexPositionColor.VertexDeclaration, m_oVertexData.Length, BufferUsage.WriteOnly);
            vb.SetData(0, m_oVertexData, 0, m_oVertexData.Length, 0);
            oDevice.SetVertexBuffer(vb);            

            m_oEffect.CurrentTechnique.Passes[0].Apply();
            oDevice.DrawPrimitives(PrimitiveType.LineList, 0, m_iNumberOfLines);
        }

        private void CreateLines()
        {
            m_oVertexData = new VertexPositionColor[0];
            List<VertexPositionColor> vertexList = new List<VertexPositionColor>();
            for (int i = 1; i < (iGRID_SIZE / iGRID_SPACING) + 1; i++)
            {
                int iSpacing = i * iGRID_SPACING;
                int iSize = iGRID_SIZE;
                Color oGridColor = Color.Gray;

                vertexList.Add(new VertexPositionColor(new Vector3(iSpacing, 0, iSize), oGridColor));
                vertexList.Add(new VertexPositionColor(new Vector3(iSpacing, 0, -iSize), oGridColor));
                vertexList.Add(new VertexPositionColor(new Vector3(-iSpacing, 0, iSize), oGridColor));
                vertexList.Add(new VertexPositionColor(new Vector3(-iSpacing, 0, -iSize), oGridColor));

                vertexList.Add(new VertexPositionColor(new Vector3(iSize, 0, iSpacing), oGridColor));
                vertexList.Add(new VertexPositionColor(new Vector3(-iSize, 0, iSpacing), oGridColor));
                vertexList.Add(new VertexPositionColor(new Vector3(iSize, 0, -iSpacing), oGridColor));
                vertexList.Add(new VertexPositionColor(new Vector3(-iSize, 0, -iSpacing), oGridColor));
            }

            // Axis Highlights
            Color oSoftAxisColor = Color.LightGray;

            // Z Axis
            vertexList.Add(new VertexPositionColor(-VectorHelper.GLOBAL_BACKWARD * iGRID_SIZE, oSoftAxisColor));
            vertexList.Add(new VertexPositionColor(Vector3.Zero * iGRID_SIZE, oSoftAxisColor));
            vertexList.Add(new VertexPositionColor(VectorHelper.GLOBAL_BACKWARD * iGRID_SIZE, Color.Blue));
            vertexList.Add(new VertexPositionColor(Vector3.Zero, Color.Blue));

            // X Axis
            vertexList.Add(new VertexPositionColor(-VectorHelper.GLOBAL_RIGHT * iGRID_SIZE, oSoftAxisColor));
            vertexList.Add(new VertexPositionColor(Vector3.Zero * iGRID_SIZE, oSoftAxisColor));
            vertexList.Add(new VertexPositionColor(VectorHelper.GLOBAL_RIGHT * iGRID_SIZE, Color.Red));
            vertexList.Add(new VertexPositionColor(Vector3.Zero, Color.Red));

            // Y Axis
            vertexList.Add(new VertexPositionColor(-VectorHelper.GLOBAL_UP * iGRID_SIZE, oSoftAxisColor));
            vertexList.Add(new VertexPositionColor(Vector3.Zero * iGRID_SIZE, oSoftAxisColor));
            vertexList.Add(new VertexPositionColor(VectorHelper.GLOBAL_UP * iGRID_SIZE, Color.Green));
            vertexList.Add(new VertexPositionColor(Vector3.Zero, Color.Green));

            m_iNumberOfLines = vertexList.Count / 2;

            // convert to array for drawing
            m_oVertexData = vertexList.ToArray();
        }

        #endregion

        #region Fields

        public const int iGRID_SIZE = 256;
        public const int iGRID_SPACING = 20;

        private int m_iNumberOfLines;
        private VertexPositionColor[] m_oVertexData;
        private BasicEffect m_oEffect;

        #endregion

    }
}
